package cn.edu.ctbu.demo1.service.impl;

import cn.edu.ctbu.demo1.domain.Student;
import cn.edu.ctbu.demo1.exception.REnum;
import cn.edu.ctbu.demo1.exception.RException;
import cn.edu.ctbu.demo1.repository.StudentRepository;
import cn.edu.ctbu.demo1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentImplService implements StudentService {

    @Autowired
    private StudentRepository studentRepository;

    public List<Student> findAll(){

        return studentRepository.findAll();
    }

    /**
     * 分页
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable) {

        return studentRepository.findAll(pageable);
    }

    /**
     * 动态
     * @param studentExample
     * @return
     */
    public List<Student>findAll(Example<Student>studentExample){

        return studentRepository.findAll(studentExample);
    }

    /**
     * 支持分页与动态查询与姓名首字匹配
     * @param student
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Student student,Pageable pageable){
        if (student == null){
            return studentRepository.findAll(pageable);
        }else {
            ExampleMatcher matcher =ExampleMatcher.matching().withMatcher("name",m->m.startsWith())
                    .withMatcher("classname",c->c.startsWith())
                    .withMatcher("age",a->a.startsWith());
            Example<Student> studentExample = Example.of(student,matcher);
            return studentRepository.findAll(studentExample,pageable);
        }
    }

    /**
     * 分页，带过滤的查询
     * @param studentExample
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Example<Student>studentExample,Pageable pageable){
        return studentRepository.findAll(studentExample,pageable);
    }

    /**
     * 按id进行查询
     * @param id 主键 整数
     * @return
     */
    public Student getById(Integer id){

        return studentRepository.findById(id).orElse(null);
    }

    /**
     * 按名字进行查询，like
     * @param name
     * @return
     */
    public List<Student> findByNameLike(String name){

        return studentRepository.findByNameLike(name);
    }

    /**
     * 按姓名和班级名称一起查询
     *
     * @param name
     * @param classname
     * @return
     */
//    public List<Student> findByNameLikeAndClassname(String name, String classname){
//
//        return studentRepository.findByNameLikeAndClassname(name, classname);
//    }
//    public Student findByNameLikeAndClassname(String name, String classname){
//
//        return studentRepository.findByNameLikeAndClassname(name, classname);
//    }

    public Student findByNameLikeAndClassname(String name, String classname) throws Exception{

        List<Student> students =  studentRepository.findByNameLikeAndClassname(name, classname);
        if(students.size()>0){
            return students.get(0);
        }else {
            throw new RException(REnum.LOGIN_ERR);
        }
    }
    public List<Student> findByNameLikeAndPassword(String name, String password){

        List<Student> students =  studentRepository.findByNameLikeAndPassword(name, password);
        return students;
    }

    public void Insert(Student student){
        studentRepository.save(student);
    }
    public void update(Student student){
        studentRepository.save(student);
    }

    public void delete(Integer id){
        studentRepository.deleteById(id);
    }

}
